package com.yl.chainStore.control.pay

import com.baomidou.mybatisplus.extension.plugins.pagination.Page
import com.yl.chainStore.common.enums.TypeEnum
import com.yl.chainStore.control.BaseCtrl
import com.yl.chainStore.entity.pay.SysCompanyPayItem
import com.yl.chainStore.entity.pay.SysCompanyPayItemType
import com.yl.chainStore.pojo.vo.pay.SysCompanyPayItemTypeSelectVo
import com.yl.chainStore.pojo.vo.pay.SystemCompanyPayItemSelectVo
import com.yl.chainStore.pojo.vo.warehouse.UpdateItemConfigVo
import com.yl.chainStore.services.IWebCompanyDeptService
import com.yl.chainStore.services.pay.ISysCompanyPayItemTypeService
import com.yl.chainStore.util.payItemTypeListToTree
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import com.yl.common.control.returnSuccess
import com.yl.common.getContext
import com.yl.users.common.annotations.PermsCode
import org.springframework.transaction.annotation.Transactional
import org.springframework.transaction.interceptor.TransactionAspectSupport
import org.springframework.web.bind.annotation.*

/**
 * 收费项目管理
 */
@RestController
@RequestMapping("pay/item")
open class PayItemController(
	private val iWebCompanyDeptService: IWebCompanyDeptService,
	private val iSysCompanyPayItemTypeService:ISysCompanyPayItemTypeService
) : BaseCtrl() {

	/**
	 * 类型树形图
	 */
	@GetMapping("typeTree")
	@Transactional
	open fun getItemTypeTree(): ResponseBody {
		return returnSuccess(
			payItemTypeListToTree(
				iWebCompanyDeptService.getItemTypeList(
					SysCompanyPayItemTypeSelectVo(deptId = getContext().subCompanyId)
				), 0L
			)
		)
	}

	/**
	 * 添加消费项目类型
	 */
	@PostMapping("addType")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:item:type:add")
	open fun addType(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		type.deptId = getContext().subCompanyId
		val addPayItemType = iWebCompanyDeptService.addPayItemType(type)
		if (addPayItemType.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addPayItemType
	}

	/**
	 * 修改消费项目类型
	 */
	@PostMapping("updateType")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:item:type:update")
	open fun updateType(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		type.deptId = getContext().subCompanyId
		val update = iSysCompanyPayItemTypeService.update(type)
		if (update.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return update
	}

	/**
	 * 修改消费项目类型
	 * 只修改名称 和 排序
	 */
	@PostMapping("updateTypeInfo")
	@PermsCode("pay:item:type:update")
	@Transactional(rollbackFor = [Exception::class])
	open fun updateTypeInfo(@RequestBody type: SysCompanyPayItemType): ResponseBody {
		val typeDetail = iSysCompanyPayItemTypeService.typeDetail(type.id)?:return returnError(ErrorCode.DATA_IS_NOT_FIND)
		typeDetail.name = type.name
		typeDetail.orderNum = type.orderNum
		val update = iSysCompanyPayItemTypeService.update(typeDetail)
		if (update.bodyIsError()) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return update
	}


	/**
	 * 删除项目类型
	 */
	@PostMapping("delType/{id}")
	@PermsCode("pay:item:type:del")
	@Transactional(rollbackFor = [Exception::class])
	open fun delType(@PathVariable id:Long):ResponseBody{
		val delPayItemType = iWebCompanyDeptService.delPayItemType(id)
		if(delPayItemType.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return delPayItemType
	}


	/**
	 * 项目分页
	 */
	@PostMapping("getItemPage")
	@PermsCode("pay:item:getItemPage")
	@Transactional
	open fun getItemPage(@RequestBody vo: SystemCompanyPayItemSelectVo): ResponseBody {
		vo.haveZeroCount = true
		return returnSuccess(iWebCompanyDeptService.getPayItemPage(Page(vo.current, vo.size), vo, getContext().subCompanyId))
	}

	/**
	 * 添加付款项目
	 */
	@PostMapping("addPayItem")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:item:add")
	open fun addPayItem(@RequestBody item: SysCompanyPayItem):ResponseBody{
		val vo = UpdateItemConfigVo(
			nickname = item.nickname,
			warningCount = item.warningCount,
			highPrice= item.highPrice,
			warningStatus =item.warningStatus,
			showLabel = item.showLabel,
				recommendPrice = item.recommendPrice
		)
		val addPayItem = iWebCompanyDeptService.addPayItem(item, vo)
		if(addPayItem.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return addPayItem
	}

	/**
	 * 修改付款项目
	 */
	@PostMapping("updatePayItem")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:item:update")
	open fun updatePayItem(@RequestBody item: SysCompanyPayItem):ResponseBody{
		val vo = UpdateItemConfigVo(
			nickname = item.nickname,
			warningCount = item.warningCount,
			highPrice= item.highPrice,
			warningStatus =item.warningStatus,
			showLabel = item.showLabel,
			itemId = item.id,
				recommendPrice = item.recommendPrice
		)
		val updatePayItem = iWebCompanyDeptService.updatePayItem(item, vo)
		if(updatePayItem.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return updatePayItem
	}

	/**
	 * 删除收费项目
	 */
	@PostMapping("delItem/{id}")
	@Transactional(rollbackFor = [Exception::class])
	@PermsCode("pay:item:del")
	open fun delItem(@PathVariable id:Long):ResponseBody{
		val delPayItem = iWebCompanyDeptService.delPayItem(id)
		if(delPayItem.bodyIsError()){
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly()
		}
		return delPayItem
	}

	/**
	 * 获取物品类型下拉
	 */
	@GetMapping("getItemTypeSelect")
	open fun getItemTypeSelect():ResponseBody{
		val resList = mutableListOf<Map<String,Any>>()
		resList.add(TypeEnum.PAY_ITEM_GOODS.getMap())
		resList.add(TypeEnum.PAY_ITEM_SERVICE.getMap())
		return returnSuccess(resList)
	}

	/**
	 * 获取物品类型下拉
	 */
	@GetMapping("getItemSelect")
	@Transactional
	open fun getItemSelect(value:String?):ResponseBody{
		val vo = SystemCompanyPayItemSelectVo()
		vo.name = value
		vo.haveZeroCount = true
		val payItemPage = iWebCompanyDeptService.getPayItemPage(Page(vo.current, vo.size), vo, getContext().subCompanyId)
		val resList = mutableListOf<Map<String,Any>>()
		for(item in payItemPage.records){
			val map = mutableMapOf<String,Any>()
			map["id"] = item.id
			map["name"] = item.itemName
			resList.add(map)
		}
		return returnSuccess(resList)
	}




}
